<template>
  <div class="container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.titleObj[state.dialog.type]"
      label-position="top" width="800px">
      <el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" label-width="auto">
        <el-form-item label="货币名称:" prop="currency_name">
          <el-input v-model.number="state.ruleForm.currency_name" placeholder="请输入" clearable
            :disabled="isDisabledFn" />
        </el-form-item>
      </el-form>
      <el-form-item label="是否显示:" prop="status">
        <el-radio-group v-model="state.ruleForm.status">
          <el-radio v-for="(item, index) in state.isOrNot" :key="index" :label="item.value">
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="!isDisabledFn" #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" size="default" @click="onSubmit">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="addMonetary">
import { reactive, onMounted, computed } from 'vue';
import { currencyAdd, currencyEdit } from '/@/api/startfab/index';
import { clueTypeArr, isOrNot, accountStatusArr, navArr } from '/@/commmon/variate';
import { ElMessage } from 'element-plus';

const emit = defineEmits(['updateList']);
const state = reactive({
  clueTypeArr: clueTypeArr,
  clueStatusArr: navArr.slice(1),
  accountStatusArr: accountStatusArr,
  isOrNot: isOrNot,
  ruleForm: {
    currency_name: '',
    status: 1,
  },
  ruleRules: {
    currency_name: [
      {
        required: true,
        message: '请输入货币名称',
        trigger: 'blur'
      },
    ]
  },
  dialog: {
    isShowDialog: false,
    row: null,
    type: '',
    titleObj: {
      'view': '查看',
      'edit': '编辑',
      'add': '新增'
    },
    isDisabled: false
  },
  loading: false,
  originalFiles: {},
})
const onSubmit = async () => {
  const params = {
    ...state.ruleForm,
  }
  console.log(params, "params");

  if (state.dialog.type === 'add') {
    currencyAdd(params).then((res) => {
      if (res.code === 10000) {
        onCancel()
        emit('updateList')
        ElMessage.success(res.msg);
      }
    })
  } else if (state.dialog.type === 'edit') {
    currencyEdit(params).then((res) => {
      if (res.code === 10000) {
        onCancel()
        emit('updateList')
        ElMessage.success(res.msg);
      }
    })
  }
}

const openDialog = (type, row) => {
  const typeArr = ['edit', 'view'];
  state.dialog.row = row;
  state.dialog.type = type;
  closeDialog();
  if (row && typeArr.includes(type)) {
    state.ruleForm = {
      ...state.ruleForm,
      id: row.id,
      currency_name: row.currency_name,
      status: row.status,
    }
  }
  state.dialog.isShowDialog = true;
};

const closeDialog = () => {
  state.ruleForm = {
    currency_name: '',
    status: 1,
  }
};

const onCancel = () => {
  closeDialog();
  state.dialog.isShowDialog = false;
};

onMounted(() => { })
const isDisabledFn = computed(() => {
  return state.dialog.type === 'view'
})
defineExpose({
  openDialog,
})
</script>
<style scoped lang="scss">
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-form-item__content) {
  display: block;
}
</style>
